package test.adddata;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-3-30 14:08
 */
public class doi合并到主表 {
    public static String fromTable = "xml_content_20200320_noexist";
    public static String fromTableParse = "pubmed_vip_extend_0328_33771437";
    public static String insertTable = "compare_doi";
    public static String insertTableSimple = "compare_doi2";

    @SneakyThrows
    public static void main(String[] args) {
        // 2022年3月30日14:08:13 思路 进行吧合并后的doi 表进行合并到主表 只更新存在且不同的情况

        // -Xmx15660m -Xms8560m
        SmallTool.printMemory();

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
        Db use = Db.use(ds);

        String allDate = "select max(pmid) as pmid  from " + insertTable;
        Entity queryNew = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + queryNew.getStr("pmid"));

        Integer count = queryNew.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            if (i <= 34) {
                continue;
            }
            SmallTool.printMemory();
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,doi_right from " + insertTable + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String doi = entity.getStr("doi_right").trim();
                Integer pmid = entity.getInt("pmid");
                updateData(pmid, doi, use);
            });
        }

    }

    @SneakyThrows
    public static void updateData(Integer pmid, String doi, Db use) {
        String sql = "select pmid , doi from " + fromTableParse + " where pmid = ?";
        Entity entity = use.queryOne(sql, pmid);
        if (entity != null) {
            Entity whereEn = new Entity();
            whereEn.setTableName(fromTableParse);
            whereEn.set("pmid", pmid);
            use.update(new Entity().set("doi", doi), whereEn);
        }


    }
}
